
var intStep=0;
//var intCnt=0;

var stepCntObj;

$(document).ready(function(){
	$("#dialog").dialog("destroy");
    $("#dialog-form").dialog({
        autoOpen: false,
        width: 430,
        modal: true,
        resizable: false,
        buttons: {
            '确定': function(){
                $(this).dialog('close');
                //btnSure();
                insertNodeConduct();
            },
            '取消': function(){
                $(this).dialog('close');
            }
        },
        close: function(){
            $("#user-form input").removeClass('invalid');
            $("#user-form label").remove('.invalid');
            $("dialog").dialog("close");
        }
    });
	$("#dialog-form1").dialog({
        autoOpen: false,
        width: 430,
        modal: true,
        resizable: false,
        buttons: {
            '确定': function(){
                $(this).dialog('close');
                //btnSure();
                insertStepCntConduct();
            },
            '取消': function(){
                $(this).dialog('close');
            }
        },
        close: function(){
            $("#user-form input").removeClass('invalid');
            $("#user-form label").remove('.invalid');
            $("dialog").dialog("close");
        }
    });
    $('#addPlanStep').click(function(){
		$("#stepName", window.frames["addPlanNode"].document).val("");
		$("#stepDesc", window.frames["addPlanNode"].document).val("");
		$("#stepNote", window.frames["addPlanNode"].document).val("");
		$('#dialog-form').dialog('open');
		//$("#stepTemplate").clone().prependTo("#planStepSure"); 
    });
	$('#subBtn').click(function(){
		nodeFinish();
    });
});



function nodeFinish(){
	//.children('')
	//alert($('#planStepSure .stepUtil').children().html());
	var json = "{\"planSteps\":[";

	var isFirst = "yes";
	
	var isFirstCnt = "yes";
	$('#planStepSure .stepUtil').each(function(i,n){
		//alert(i);
		//alert(n.html());
		//alert($(this).html());
		//alert($('$(this) .stepName').text());
		//alert($(this).find('.stepName').text()); succ
		if (isFirst == "yes") {
            isFirst = "no";
            json += "{\"stepName\":";
		}else{
			json += "},{\"stepName\":";
		}
        json += "\"";
        json += $(this).find('.stepName').text();
        json += "\"";
		//
		json += ",";
        json += "\"stepDesc\":";
        json += "\"";
        json += $(this).find('.stepDesc').text();
        json += "\"";
		//
		json += ",";
        json += "\"stepNote\":";
        json += "\"";
        json += $(this).find('.stepNote').text();
        json += "\"";
		//
		json += ",";
        json += "\"stepCnts\":[";
		$(this).find('.cntUtil').each(function(i,n){
			//alert($(this).find('.tbLogType').text());
			if (isFirstCnt == "yes") {
	            isFirstCnt = "no";
	            json += "{\"tbLogType\":";
			}else{
				json += "},{\"tbLogType\":";
			}
			json += "\"";
			//alert($(this).find('.tbLogTypeID').text());
	        json += $(this).find('.tbLogTypeID').text();
	        json += "\"";
			//pscContent
			json += ",";
	        json += "\"pscContent\":";
	        json += "\"";
	        json += $(this).find('.pscContent').text();
	        json += "\"";
		});
		json += "}]";
		isFirstCnt = "yes";
	});
	json += "}]}";
	savePlanSteps(json);
	//json = null;
	//alert(json);
    //alert(eval(json));
	//document.write(json);
	//alert(eval(json));
    //alert('('+eval(json)+')');
	
	//var d="";
    //json="{'planSteps':[";
}

function savePlanSteps(planSteps){
	//alert($('#planID').text());
    var url = 'lzm/SavePlan_savePlanSteps.action';
    var timestamp = Date.parse(new Date());
    var params = {
        'timestamp': timestamp,
        'planSteps': planSteps,
		'planID':$('#planID').text()
    };
    jQuery.post(url, params, savePlanStepsRst, 'json');
}

/**
 * 作增加预案回调函数
 */
function savePlanStepsRst(data){
    if (data.ajaxRst == SUCCESS) {
        //operateSucc("/ag/lzm/save_plan.jsp");
		alert("定义预案工作流成功！");
		window.location.href="save_plan.jsp";
    }
    else {
        alert("定义预案工作流失败，请重试！");
    }
}

function insertNodeConduct(){
	//alert("dfsdf");
	//$('#intStep').text
	var tempObj1=$("#stepTemplate").clone();
	var tempObj2;
	tempObj2=tempObj1;
	intStep++;
	tempObj2.find('.intStepSP').text("环节"+intStep);
	tempObj2.find('.stepName').text($("#stepName", window.frames["addPlanNode"].document).val());
	tempObj2.find('.stepDesc').text($("#stepDesc", window.frames["addPlanNode"].document).val());
	tempObj2.find('.stepNote').text($("#stepNote", window.frames["addPlanNode"].document).val());
	tempObj2.find('.divBorder1').html("");
	tempObj2.find('.countTag').text(0);	
	//$('#planStepCntSure').html("");
	//tempObj.prependTo("#planStepSure"); 
	$('#planStepSure').append(tempObj2); 
	$('.addPlanStepCnt').unbind("click");
	$('.addPlanStepCnt').click(function(){
		stepCntObj=$(this);
		addPlanStepCnt();
    });
}

function insertStepCntConduct(){
	//alert("insertStepCntConduct");
	var tempObj1=$("#stepCntTemplate").clone();
	var tempObj2;
	tempObj2=tempObj1;
	//intCnt++;
	var intCnt=parseInt(stepCntObj.prev().text());
	intCnt++;
	stepCntObj.prev().text(intCnt);
	tempObj2.find('.intCntSP').text("环节内容"+intCnt);
	tempObj2.find('.tbLogType').text($("#tbLogType option:selected'", window.frames["addPlanNode1"].document).text());
	tempObj2.find('.tbLogTypeID').text($("#tbLogType option:selected'", window.frames["addPlanNode1"].document).val());
	tempObj2.find('.pscContent').text($("#pscContent", window.frames["addPlanNode1"].document).val());
	stepCntObj.next().append(tempObj2);
	//$('#planStepCntSure').append(tempObj2);
	//tempObj2.prependTo("#planStepCntSure"); addStepCnt
}

function addPlanStepCnt(){
	//alert("fdgdfg");
	//$("#tbLogType", window.frames["addPlanNode1"].document).val("");
	$("#pscContent", window.frames["addPlanNode1"].document).val("");
	$('#dialog-form1').dialog('open');
	//$("#stepCntTemplate").clone().prependTo("#planStepCntSure"); 
}
